package fr.pud.client.controller;

import java.awt.event.MouseEvent;

import org.openstreetmap.gui.jmapviewer.DefaultMapController;

import fr.pud.client.view.JMapViewerExtended;
import fr.pud.client.view.mapstation.Bubble;
import fr.pud.client.view.mapstation.BubbleSubBackground;

/**
 * Sets the behavior of the map (JMapViewer) up.
 * Apr 9, 2012 - OSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         MapController.java
 */
public class MapController extends DefaultMapController {
    /**
     * A constructor which extended the constructor of DefaultMapController
     * @param map
     */
    public MapController(JMapViewerExtended map) {
        super(map);
        this.setMovementMouseButton(MouseEvent.BUTTON1);
    }

    /**
     * If the right button is pressed, the context menu is display <br />
     * If a double left-click is detected the zoom is increased. <br />
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        ((JMapViewerExtended) this.map).getLocation(e.getLocationOnScreen());
        if (this.isDoubleClickZoomEnabled() && e.getClickCount() == 2
                && e.getButton() == MouseEvent.BUTTON1) {
            this.map.zoomIn(e.getPoint());
        }
        if (e.getButton() == MouseEvent.BUTTON3) {
            ((JMapViewerExtended) this.map).getContextMenu().setLocation(
                    e.getLocationOnScreen());
            ((JMapViewerExtended) this.map).getContextMenu().setVisible(true);
        }
        if (e.getButton() == MouseEvent.BUTTON1) {
            ((JMapViewerExtended) this.map).getContextMenu().setVisible(false);
        }
    }

    /**
     * If the mouse is dragged the map and the pointers are repainted.
     */
    @Override
    public void mouseDragged(MouseEvent arg0) {
        super.mouseDragged(arg0);
        for (int i = BubbleSubBackground.RIGHT_SUB_BUBBLE; i <= BubbleSubBackground.CENTER_SUB_BUBBLE; i++) {
            Bubble.getBubble(i).setVisible(false);
        }
        JMapViewerExtended.getJMapViewer().setMapMarkerVisible(true);
        JMapViewerExtended.getJMapViewer().moveMap(0, 1);
    }
}
